/*
 * Settings.java
 *
 * Copyright 2007, 2008 Tiberiumlabs
 *
 * This file is part of Tiberiumlabs Lailaps.
 *
 * Tiberiumlabs Lailaps is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Tiberiumlabs Lailaps is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.tiberiumlabs.lailaps.settings;

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import org.tiberiumlabs.lailaps.gui.elements.ExceptionDialog;

/**
 *
 * @author <a href="mailto:paranoid.tiberiumlabs@gmail.com">Paranoid</a>
 */
public class Settings {
    
    // <editor-fold defaultstate="collapsed" desc=" settings files ">
    private static final String getSettingsFileApplication() {
        return ApplicationSettings.SETTINGS_FOLDER + "application.xml.gz";
    }
    private static final String getSettingsFileNetwork() {
        return ApplicationSettings.SETTINGS_FOLDER + "network.xml.gz";
    }
    private static final String getSettingsFileInterface() {
        return ApplicationSettings.SETTINGS_FOLDER + "interface.xml.gz";
    }
    public static final String getPortFile() {
        return ApplicationSettings.SETTINGS_FOLDER + "port";
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc=" private settings beans ">
    private static ApplicationSettingsImpl applicationSettings = null;
    private static NetworkSettingsImpl networkSettings = null;
    private static InterfaceSettingsImpl interfaceSettings = null;
    
    public static ApplicationSettings getApplicationSettings() {
        return applicationSettings;
    }
    public static NetworkSettings getNetworkSettings() {
        return networkSettings;
    }
    public static InterfaceSettings getInterfaceSettings() {
        return interfaceSettings;
    }
    
    public static ApplicationSettingsImpl getEditableApplicationSettings() {
        return applicationSettings;
    }
    public static NetworkSettingsImpl getEditableNetworkSettings() {
        return networkSettings;
    }
    public static InterfaceSettingsImpl getEditableInterfaceSettings() {
        return interfaceSettings;
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc=" saving/loading settings ">
    public static boolean loadSettings() {
        boolean allFilesLoaded = true;
        
        applicationSettings = (ApplicationSettingsImpl)loadFile(getSettingsFileApplication());
        if (applicationSettings == null) {
            applicationSettings = new ApplicationSettingsImpl();
            saveFile(getSettingsFileApplication(), applicationSettings);
            allFilesLoaded = false;
        }
        
        networkSettings = (NetworkSettingsImpl)loadFile(getSettingsFileNetwork());
        if (networkSettings == null) {
            networkSettings = new NetworkSettingsImpl();
            saveFile(getSettingsFileNetwork(), networkSettings);
            allFilesLoaded = false;
        }
        
        interfaceSettings = (InterfaceSettingsImpl)loadFile(getSettingsFileInterface());
        if (interfaceSettings == null) {
            interfaceSettings = new InterfaceSettingsImpl();
            saveFile(getSettingsFileInterface(), interfaceSettings);
            allFilesLoaded = false;
        }
        
        return allFilesLoaded;
    }
    
    public static void saveApplicationSettings() {
        saveFile(getSettingsFileApplication(), applicationSettings);
        savePort(applicationSettings.getPort());
    }
    
    public static void saveNetworkSettings() {
        saveFile(getSettingsFileNetwork(), networkSettings);
    }
    
    public static void saveInterfaceSettings() {
        saveFile(getSettingsFileInterface(), interfaceSettings);
    }
    
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc=" saving/loading files ">
    private static void saveFile(String fileName, Object targetObject) {
        try {
            File f = new File(fileName);
            if (!f.exists()) {
                f.getParentFile().mkdirs();
            } else {
                f.delete();
            }
            f.createNewFile();
            XMLEncoder out = new XMLEncoder(
                    new GZIPOutputStream(
                    new BufferedOutputStream(
                    new FileOutputStream(fileName)
                    )
                    )
                    );
            out.writeObject(targetObject);
            out.flush();
            out.close();
        } catch (Exception ex) {
            new ExceptionDialog(ex);
        }
    }
    private static Object loadFile(String fileName) {
        Object result = null;
        try {
            File f = new File(fileName);
            if (f.exists()) {
                XMLDecoder in = new XMLDecoder(
                        new GZIPInputStream(
                        new BufferedInputStream(
                        new FileInputStream(fileName)
                        )
                        )
                        );
                result = in.readObject();
                in.close();
            }
        } catch (Exception ex) {
            new ExceptionDialog(ex);
        }
        return result;
    }
    
    private static void savePort(int port) {
        try {
            
            FileOutputStream portFile = new FileOutputStream(getPortFile());
            
            portFile.write(Integer.toString(port).getBytes());
            
            portFile.flush();
            portFile.close();
            
        } catch (Exception e) {
            new ExceptionDialog(e);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc=" debug mode ">
    
    private static boolean debugMode = "on".equals(System.getProperty("lailaps.debug"));

    public static boolean isDebugMode() {
        return debugMode;
    }

    // </editor-fold>
    
}
